* to execute, nohup stata -b do melly_decomp_inc5 &
        

set mem 2000m
set matsize 500

* LOAD RAW DATA

infile ref_year msupwgt r_inc1 r_inc2 r_inc6 dem_grp emp_head age_ref race1 race2 race3 fam1 fam2 fam3 fam4 fam5 ed1 ed2 ed3 ed4 using "~/jim/inequality/output/melly6317.dat"

foreach x in r_inc1 r_inc2 r_inc6 {
gen ln_`x'=ln(`x') if `x'>0
}

gen age_ref_2=age_ref*age_ref
gen age_ref_3=age_ref_2*age_ref 

* 1963 HAS 8 OBS WITH NEGATIVE WGT, 1972 HAS 85;
replace msupwgt=0 if msupwgt<0

*********************************
* RUN DECOMPOSITION FOR 1963-1972
*********************************
preserve
keep if ref_year==1963 | ref_year==1972
gen groupvar=1
replace groupvar=0 if ref_year==1963
set seed 1

gen small_samp=uniform()
keep if small_samp<.33

rqdeco3 ln_r_inc2 age_ref age_ref_2 age_ref_3 fam2 fam3 fam4 fam5 race1 ed2 ed3 ed4 [pw=msupwgt], by(groupvar) qlow(.1) qhigh(.9) qstep(.4) 

restore


*********************************
* RUN DECOMPOSITION FOR 1972-1980
*********************************
preserve
keep if ref_year==1972 | ref_year==1980
gen groupvar=1
replace groupvar=0 if ref_year==1972
set seed 1

gen small_samp=uniform()
keep if small_samp<.33

rqdeco3 ln_r_inc2 age_ref age_ref_2 age_ref_3 fam2 fam3 fam4 fam5 race1 ed2 ed3 ed4 [pw=msupwgt], by(groupvar) qlow(.1) qhigh(.9) qstep(.4) 

restore


*********************************
* RUN DECOMPOSITION FOR 1980-1990
*********************************
preserve
keep if ref_year==1980 | ref_year==1990
gen groupvar=1
replace groupvar=0 if ref_year==1980
set seed 1

gen small_samp=uniform()
keep if small_samp<.33

rqdeco3 ln_r_inc2 age_ref age_ref_2 age_ref_3 fam2 fam3 fam4 fam5 race1 ed2 ed3 ed4 [pw=msupwgt], by(groupvar) qlow(.1) qhigh(.9) qstep(.4) 

restore


*********************************
* RUN DECOMPOSITION FOR 1990-2000
*********************************
preserve
keep if ref_year==1990 | ref_year==2000
gen groupvar=1
replace groupvar=0 if ref_year==1990
set seed 1

gen small_samp=uniform()
keep if small_samp<.33

rqdeco3 ln_r_inc2 age_ref age_ref_2 age_ref_3 fam2 fam3 fam4 fam5 race1 ed2 ed3 ed4 [pw=msupwgt], by(groupvar) qlow(.1) qhigh(.9) qstep(.4) 

restore


*********************************
* RUN DECOMPOSITION FOR 2000-2017
*********************************
preserve
keep if ref_year==2000 | ref_year==2017
gen groupvar=1
replace groupvar=0 if ref_year==2000
set seed 1

gen small_samp=uniform()
keep if small_samp<.33

rqdeco3 ln_r_inc2 age_ref age_ref_2 age_ref_3 fam2 fam3 fam4 fam5 race1 ed2 ed3 ed4 [pw=msupwgt], by(groupvar) qlow(.1) qhigh(.9) qstep(.4) 

restore



